/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package triploa;

import org.kxml2.io.*;
import org.kxml2.kdom.*;
import org.xmlpull.v1.*;
import java.io.*;
import de.enough.polish.util.Locale;

/**
 *
 * @author User
 */
public class UserProfile {

    private String username, password, name, surname;
    private String address;
    private String city;
    private String email;
    private String phoneNumber;
    private String smoker;
    private String birthdate;
    private String gender;
    private int age;
    private boolean license=false;
    private int id;
    private float feedback;
    private static final String ID_TAG="a:UserID";
    private static final String PROFILE_TAG="a:UserInfo";
    private static final String PROFILE2_TAG="a:ProfileInfo";
    private static final String USERNAME_TAG="a:Nickname";
    private static final String USERNAME2_TAG="a:Username";
    private static final String NAME_TAG="a:Name";
    private static final String SURNAME_TAG="a:Surname";
    private static final String ADDRESS_TAG="a:Address";
    private static final String AGE_TAG="a:Age";
    private static final String CITY_TAG="a:City";
    private static final String LICENSE_TAG="a:DrivingLicense";
    private static final String EMAIL_TAG="a:Email";
    private static final String PHONE_TAG="a:PhoneNumber";
    private static final String SMOKER_TAG="a:Smoker";
    private static final String BANNED_TAG="a:BannedList";
    private static final String BIRTH_TAG="a:Birthday";
    private static final String BUDDY_TAG="a:BuddyList";
    private static final String INTERESTS_TAG="a:Interests";
    private static final String GENDER_TAG="a:Gender";
    private static final String VEHICLE_TAG="a:VehicleDetails";
    private static final String FEEDBACKS_TAG="a:Feedbacks";
    
    private static final String FEEDBACK_TAG="a:NumericFeedbacks";
    
    private static final String _TAG="password";
    private UserProfile() {

    }

    UserProfile(String usr, String pwd, String nm, String sn) {
        username = usr;
        name = nm;
        password = pwd;
        surname = sn;
    }
/**
 * call for getting a new instance of this class from xml
 * @param p Parser
 * @return a new instance of FeedBack
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
    public static UserProfile create(KXmlParser p) throws XmlPullParserException, IOException {
        UserProfile up = new UserProfile();
        up.update(p);
        if(up.username==null) return null;
        return up;
    }
    FeedBack feed;
    /**
 * Update this object class from xml parser
 * @param p Parser
 * @throws java.io.IOException
 * @throws org.xmlpull.v1.XmlPullParserException
 */
    public void update(KXmlParser p) throws XmlPullParserException, IOException {
        boolean exit = false, travel = false;
        int token = -1;
        String n="",lasttag=n;
        while (!exit) {
            token = p.next();
            switch (token) {
                case KXmlParser.START_DOCUMENT:
                    break;
                case KXmlParser.END_DOCUMENT:
                    System.out.println("Fine Documento");
                    exit = true;  //exit form while
                    break;
                case KXmlParser.START_TAG:
                    System.out.println(p.getName());
                    if (p.getName().equals(PROFILE_TAG)||p.getName().equals(PROFILE2_TAG)) {
                        travel = true;
                    }
                    if (travel) {
                        lasttag=p.getName();
                        if (lasttag.equals(FEEDBACKS_TAG)) {
                            //feed=FeedBack.create(p);
                        }
                    }
                    break;
                case KXmlParser.END_TAG:
                    if (travel) {
                        if (p.getName().equals(PROFILE_TAG)||p.getName().equals(PROFILE2_TAG)) {
                            System.out.println("Fine tag");
                            travel = false;
                            exit = true;
                            break;
                        }
                        
                    }
                    if(p.getName().equals("a:ReservatedUsers")) exit=true;
                    break;
                case KXmlParser.TEXT:
                    if (lasttag.equals(USERNAME_TAG)||lasttag.equals(USERNAME2_TAG)) {
                        setUsername(p.getText());
                    }
                    if (lasttag.equals(NAME_TAG)) {
                        setName(p.getText());
                    }
                    if (lasttag.equals(SURNAME_TAG)) {
                        setSurname(p.getText());
                    }
                    /*if (lasttag.equals(_TAG)) {
                        String dt= p.getText();
                        int t=dt.indexOf("T");
                        date = dt.substring(0, t);
                        setTime(dt.substring(t + 1));
                    }*/
                    if (lasttag.equals(FEEDBACK_TAG)) {
                         feedback= Float.parseFloat(p.getText());
                    }
                    if (lasttag.equals(ID_TAG)) {
                        
                         setId(Integer.parseInt(p.getText()));
                    }
                    if (lasttag.equals(ADDRESS_TAG)) {
                         setAddress(p.getText());
                    }
                    if (lasttag.equals(AGE_TAG)) {
                         setAge(Integer.parseInt(p.getText()));
                    }
                    if (lasttag.equals(CITY_TAG)) {
                         setCity(p.getText());
                    }
                    if (lasttag.equals(EMAIL_TAG)) {
                         setEmail(p.getText());
                    }
                    if (lasttag.equals(PHONE_TAG)) {
                         setPhoneNumber(p.getText());
                    }
                    if (lasttag.equals(LICENSE_TAG)) {
                         
                        if(p.getText().equals("true"))
                        {
                            license=true;
                        }
                    }
                    if (lasttag.equals(SMOKER_TAG)) {
                         setSmoker(p.getText());
                         if(getSmoker().equals("Unknown"))
                         {
                             setSmoker(Locale.get("Unknown"));
                         }
                         if(getSmoker().equals("Smoker"))
                         {
                             setSmoker(Locale.get("Smoker"));
                         }
                         if(getSmoker().equals("NotSmoker"))
                         {
                             setSmoker(Locale.get("NotSmoker"));
                         }
                    }
                    if (lasttag.equals(BIRTH_TAG)) {
                         setBirthdate(p.getText());
                         int time=getBirthdate().indexOf('T'); //si prende solo la data
                         setBirthdate(getBirthdate().substring(0, time - 1));
                    }
                    if (lasttag.equals(GENDER_TAG)) {
                         setGender(p.getText());
                         if(getGender().equals("Unknown"))
                         {
                             setGender(Locale.get("Unknown"));
                         }
                         if(getGender().equals("Male"))
                         {
                             setGender(Locale.get("Male"));
                         }
                         if(getGender().equals("Female"))
                         {
                             setGender(Locale.get("Female"));
                         }
                    }
                    break;
            }
        }
    }

    public int getId() {
        return id;
    }

    public float getFeedback() {
        return feedback;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getSmoker() {
        return smoker;
    }

    public void setSmoker(String smoker) {
        this.smoker = smoker;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public boolean isLicense() {
        return license;
    }

    public void setLicense(boolean license) {
        this.license = license;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setId(int id) {
        this.id = id;
    }
}
